** Preparing leader characteristics on the year after the election

use "$project_path/data/3_cleaned/outcomes", clear
keep Country Year age_hos left_hos populist_hos illiberal_hos age_hog left_hog populist_hog illiberal_hog
egen country_id = group(Country)
xtset country_id Year
foreach v in age_hos left_hos populist_hos illiberal_hos age_hog left_hog populist_hog illiberal_hog {
	gen post_elec_`v' = F.`v'
}
keep Country Year post_elec_*

merge 1:m Country Year using "$project_path/data/4_regdata/regdata", keep(using match) nogen

foreach v in age left populist illiberal {
	gen post_elec_`v' = .
	replace post_elec_`v' = post_elec_`v'_hos if elected_leader=="HOS"
	replace post_elec_`v' = post_elec_`v'_hog if elected_leader=="HOG"
}

** Appendix Figure E.8(a)

rdrobust post_elec_age runvar
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot post_elec_age runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) ytitle("", size(large)) legend(off) title("Leader age (in years)", size(large)))
graph export "$project_path/output/appendix_figures/Appendix_Figure_E8a.pdf", as(pdf) name("Graph") replace

** Appendix Figure E.8(b)

rdrobust post_elec_left runvar
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot post_elec_left runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) ytitle("V-Party score", size(large)) legend(off) title("Party's left-right score", size(large)))
graph export "$project_path/output/appendix_figures/Appendix_Figure_E8b.pdf", as(pdf) name("Graph") replace

** Appendix Figure E.8(c)

rdrobust post_elec_populist runvar
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot post_elec_populist runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) ytitle("0-1 scale", size(large)) legend(off) title("Party's populism score", size(large)))
graph export "$project_path/output/appendix_figures/Appendix_Figure_E8c.pdf", as(pdf) name("Graph") replace

** Appendix Figure E.8(d)

rdrobust post_elec_illiberal runvar
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot post_elec_illiberal runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) ytitle("0-1 scale", size(large)) legend(off) title("Party's illiberalism score", size(large)))
graph export "$project_path/output/appendix_figures/Appendix_Figure_E8d.pdf", as(pdf) name("Graph") replace


